from dm import dm
import time
import random
import toogleRoom as tg
import user as us
import sthz as gb
from util import check_has_pl,move_click,move_click_with_xd,yx_start_pos


def search_kly():
    klyd = dm.FindPic(0,0,1024,768,"./nyz_dt/klyd.bmp","123456",1,0)
    if klyd[0]!=-1:
        move_click(int(klyd[1])+48,int(klyd[2]),yx_start_pos)
        time.sleep(1)
        for i in range(random.randrange(1,4)):
            gb.pressandreleasekeybyvalue(32)
            time.sleep(random.uniform(0.3,0.7))
        return
    else:
        move_click(452,20,yx_start_pos)
        search_kly()

def toogle_js():
    hbl_mk  = dm.FindPic(0,0,1024,768,"./nyz_dt/hbl_mk.bmp|./nyz_dt/hbl_mk2.bmp|./nyz_dt/hbl_mk3.bmp","123456",1,0)
    if hbl_mk[0]!=-1:
        print(hbl_mk,'在海博伦门口')
        while True:
            rw = dm.FindPic(0,0,1024,768,"./nyz_dt/cwrw.bmp","123456",0.9,3)
            if rw[0]==-1:
                print("点击小铃铛")
                move_click_with_xd(609,576,yx_start_pos)
                time.sleep(random.uniform(0.3,0.7))
            else:
                break
        while True:
            mrrw = dm.FindPic(0,0,1024,768,"./nyz_dt/mrrw.bmp|./nyz_dt/mrrw1.bmp|./nyz_dt/mrrw2.bmp","123456",1,1)
            if mrrw[0]==-1:
                print('找不到每日任务')
                move_click(rw[1],rw[2],yx_start_pos)
                time.sleep(1)
            else:
                print("找到每日任务")
                break
        while True:
            lq = dm.FindPic(0,0,1024,768,"./nyz_dt/lq1.bmp|./nyz_dt/lq2.bmp|./nyz_dt/lq3.bmp|./nyz_dt/lq4.bmp","123456",1,0)
            if lq[0]==-1:
                print('领取完每日任务')
                time.sleep(1)
                break
            else:
                move_click(lq[1],lq[2],yx_start_pos)
                time.sleep(2)
    while True:
        i=0
        ksyx = dm.FindPic(0,0,1024,768,"./nyz_dt/ksyx.bmp","123456",1,0)
        qhjsym = dm.FindPic(0,0,1024,768,"./nyz_dt/qhjsym.bmp","123456",1,0)
        print(qhjsym,'切换角色')
        print(ksyx,'开始游戏')
        if qhjsym[0]!=-1:
            move_click_with_xd(380,494,yx_start_pos)
            gb.movemouseto(1027,768)
            time.sleep(random.randrange(1,3))
        else:
            gb.pressandreleasekeybyvalue(27)
            time.sleep(2) 
        
        if ksyx[0]!=-1:
            gb.pressandreleasekeybyvalue(39)
            time.sleep(random.randrange(1,3))
            gb.pressandreleasekeybyvalue(32)
            while True:
                slyfj = dm.FindPic(0,0,1024,768,"./nyz_dt/slyfj.bmp","123456",0.9,0)
                if slyfj[0]!=-1:
                    return
                else:
                    print('没有进入赛利亚房间')
                    if i==0:
                        gb.pressandreleasekeybyvalue(27) 
                    else:
                        break
                    i+=1
            break
        

def go_hbl():
    gb.pressandreleasekeybyvalue(27)
    esc = dm.FindPic(0,0,1024,768,"./nyz_dt/esc.bmp","123456",1,2)
    if esc[0]!=-1:
        gb.pressandreleasekeybyvalue(27)
    is_press_left = False
    gb.pressandreleasekeybyvalue(74)
    time.sleep(random.uniform(0.3,0.7))
    gb.pressandreleasekeybyvalue(27)
    time.sleep(random.uniform(1,3))
    # 判断是否有疲劳
    is_pl = check_has_pl(dm)
    if not is_pl:
       print('人物没疲劳')
       toogle_js()
       go_hbl()
    else:
        esc = dm.FindPic(0,0,1024,768,"./nyz_dt/esc.bmp","123456",1,2)
        if esc[0]!=-1:
            gb.pressandreleasekeybyvalue(27)
        for i in range(10):
            rw = dm.FindPic(0,0,1024,768,"./nyz_dt/rw.bmp|./nyz_dt/cwrw.bmp","123456",0.8,0)
            print(rw,'xiaolingdang ')
            if rw[0]!=-1:
                print("点击小铃铛")
                move_click_with_xd(609,576,yx_start_pos)
                time.sleep(random.uniform(0.3,0.7))
                move_click_with_xd(616,196,yx_start_pos)
                time.sleep(random.uniform(0.3,0.7))
                move_click_with_xd(617,244,yx_start_pos)
                time.sleep(random.uniform(0.3,0.7))
                break
        bz_slyfj = False
        while True:
            hbl_mk  = dm.FindPic(0,0,1024,768,"./nyz_dt/hbl_mk.bmp|./nyz_dt/hbl_mk2.bmp|./nyz_dt/hbl_mk3.bmp","123456",1,0)
            fc = dm.FindPic(0,0,1024,768,"./nyz_dt/fc.bmp|./nyz_dt/fc2.bmp","123456",1,0)
            slyfj = dm.FindPic(0,0,1024,768,"./nyz_dt/slyfj.bmp","123456",0.9,0)
            if slyfj[0]!=-1:
                while True:
                    dtxz = dm.FindPic(0,0,1024,768,"./nyz_dt/cxdtxz.bmp","123456",0.9,0)
                    if dtxz[0]==-1:
                        if not is_press_left:
                            print("没有出现选图齿轮")
                            gb.presskeybyvalue(39)
                            is_press_left=True
                    else:
                        print("出现选图齿轮")
                        gb.releasekeybyvalue(39)
                        is_press_left=False
                        break
                while True:
                    blxer = dm.FindPic(0,0,1024,768,"./nyz_dt/dtxz.bmp","123456",0.9,0)
                    if blxer[0]==-1:
                        print("没有找到去飞船")
                        gb.pressandreleasekeybyvalue(40)
                    else:
                        print("去飞船")
                        move_click(int(blxer[1]+random.randrange(80,120)),int(blxer[2])+random.randrange(10,20),yx_start_pos)
                        time.sleep(random.randrange(2,4))
                        gb.movemouseto(1027,768)
                        bz_slyfj=True
                        break
            else:
                if not bz_slyfj and hbl_mk[0]==-1:
                    print('按esc')
                    gb.pressandreleasekeybyvalue(27)
                    time.sleep(1)       
            if hbl_mk[0]!=-1:
                 while True:
                    xt  = dm.FindPic(0,0,1024,768,"./nyz_dt/xt.bmp","123456",1,0)
                    if xt[0]==-1:
                        if not is_press_left:
                            gb.presskeybyvalue(39)
                            is_press_left=True
                    else:
                        gb.releasekeybyvalue(39)
                        is_press_left=False
                        break
                 while True:
                    hbl_dt = dm.FindPic(0,0,1024,768,"./nyz_dt/hbl_dt.bmp","123456",1,0)
                    if hbl_dt[0]==-1:
                        gb.pressandreleasekeybyvalue(40)
                        time.sleep(random.uniform(0.3,0.5))
                    else:
                        print('重新开始游戏')
                        while True:
                            boss = dm.FindPic(0,0,1024,768,'./nyz_dt/boss.bmp',"123456",1,2)
                            if boss[0]==-1:
                                gb.pressandreleasekeybyvalue(32)
                            else:
                                tg.before_start()
                                return
                        
            if fc[0]!=-1:
                 print("到达飞船")
                #  gb.movemouserelative(20,20)
                 move_click(int(fc[1]),int(fc[2])-random.randrange(110,130),yx_start_pos)
                 time.sleep(1)
                 fc2 = dm.FindPic(0,0,1024,768,"./nyz_dt/fc.bmp|./nyz_dt/fc2.bmp","123456",1,0)
                 if fc2[0] ==-1:
                     print("打开飞船面板")
                     search_kly()